/**
 *
 */
package com.cwt_taxi_xa.record;

import android.media.MediaRecorder;
import android.media.MediaRecorder.OnErrorListener;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.model.RecorderInfo;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.ExceptionUtil;

import java.io.File;

/**
 * @author wlh
 * 系统录音
 */
public class SystemRecord {

	private static final String TAG = "SystemRecord";
	public static MediaRecorder mediaRecorder;
	public static boolean isRecording;
	private static RecorderInfo info;
	/*private static RecordTimer task;
	private static Timer timer;*/

	public static void doRecord(RecorderInfo info){
		SystemRecord.info = info;
		if(isRecording){
			stop(true);
		}
		start();
	}

	/*private static void stopTimer(){
		if(timer != null){
			timer.cancel();
			timer = null;
		}
		if(task != null){
			task.cancel();
			task = null;
		}
	}

	private static void starTimer(){
		stopTimer();
		if(timer == null){
			timer = new Timer();
		}
		if(task == null){
			task = new RecordTimer();
		}
		timer.schedule(task, 60*1000, 60000);
	}*/

	/**
	 * 开始录音
	 */
	private static void start() {

		try {
			//查看SD卡剩余内存情况
			/*try {
				long size = FileOper.getSDFreeSize(Constants.RAM_PATH);
				if(size<100){
					AppLog.v(TAG, "SD卡未空间少于200M");
					return;
				}

			} catch (Exception e) {
				AppLog.e(ExceptionUtil.getInfo(e), e);
				e.printStackTrace();
			}*/

			//检查文件路径是否存在
			File filePath = new File(Constants.RECORDING_PATH);
			if(!filePath.exists()){
				filePath.mkdirs();
			}

			AppLog.v(TAG, "录音开始");
			//			starTimer();
			mediaRecorder = new MediaRecorder();
			// 设置音频录入源
			mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			// 设置录制音频的输出格式
			mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
			//RAW_AMR
			// 设置音频的编码格式
			mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			// 设置录制音频文件输出文件路径
			mediaRecorder.setOutputFile(Constants.RECORDING_PATH+info.getTime()+".mp3");
			if(RecorderBiz.getRecordType() == 1){
				mediaRecorder.setMaxDuration(RecorderBiz.getMaxTime()*60*1000);// 最长录音时间
			}else{
				mediaRecorder.setMaxDuration(720*60*1000);// 最长录音时间 720分钟
			}
			mediaRecorder.setOnErrorListener(new OnErrorListener() {

				@Override
				public void onError(MediaRecorder mr, int what, int extra) {
					// 发生错误，停止录制
					mediaRecorder.stop();
					mediaRecorder.release();
					mediaRecorder = null;
					isRecording=false;

					//录音错误重新启动录音
					start();
				}
			});

			RecorderBiz recordingBiz = new RecorderBiz(MainApplication.getInstance());
			// 跟新最后一条数据的结束录音时间
			if(RecorderBiz.getRecordType() == 1){
				recordingBiz.updateLastEndTime();
			}
			// 添加新的录音
			recordingBiz.addInfo(info);
			AppLog.i(TAG, "录音文件ID"+info.getMediaId());

			// 准备、开始
			mediaRecorder.prepare();
			mediaRecorder.start();

			isRecording=true;
		} catch (Exception e) {
			AppLog.e(ExceptionUtil.getInfo(e), e);
			e.printStackTrace();
			stop(false);
		}
	}

	/**
	 * 录音结束
	 */
	public static void stop(boolean updateEndTime){
		//AppLog.v(TAG, "录音：结束录音，isRecording："+isRecording+"; updateEndTime:"+updateEndTime);
		if (isRecording) {
			AppLog.v(TAG, "结束录音");
			isRecording=false;
			if(updateEndTime){
				updateEndTime();
			}
			// 如果正在录音，停止并释放资源
			try {
				mediaRecorder.stop();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				AppLog.i(TAG, "结束录音出错");
			}
			try {
				mediaRecorder.release();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				AppLog.i(TAG, "释放录音出错");
			}
			mediaRecorder = null;
		}
		//		stopTimer();
	}

	public static void updateEndTime(){
		RecorderBiz recordingBiz = new RecorderBiz(MainApplication.getInstance());
		recordingBiz.updateLastEndTime();
	}
}
